﻿//using System;
//using System.Collections.Generic;
//using System.Data.Entity;
//using System.Linq;
//using UCosmic.Domain;
//using UCosmic.Domain.Email;
//using UCosmic.Orm;

//namespace UCosmic.Storage
//{
//    public class EmailTemplateStorage : IStoreEmailTemplates
//    {
//        #region Construction & DI

//        public IUnitOfWork UnitOfWork { get; private set; }

//        private UCosmicContext DbContext
//        {
//            get { return UnitOfWork as UCosmicContext; }
//        }

//        public EmailTemplateStorage(IUnitOfWork unitOfWork)
//        {
//            UnitOfWork = unitOfWork;
//        }

//        #endregion
//        #region Get / Read

//        public IEnumerable<EmailTemplate> Find(EmailTemplateFinder finder)
//        {
//            // do not track entities returned by find methods
//            var query = DbContext.EmailTemplates.AsQueryable();

//            // initialize finder
//            if (finder == null)
//                finder = new EmailTemplateFinder();

//            // apply includes
//            var includes = finder.ToBeEagerLoaded;
//            if (includes != null && includes.Count > 0)
//                query = finder.ToBeEagerLoaded.Aggregate(query, (lastInclude, nextInclude) =>
//                    lastInclude.Include(nextInclude));

//            // apply current, archived, and deleted switches
//            if (finder.IsCurrent.HasValue)
//                query = query.Where(role => role.IsCurrent == finder.IsCurrent.Value);
//            if (finder.IsArchived.HasValue)
//                query = query.Where(role => role.IsArchived == finder.IsArchived.Value);
//            if (finder.IsDeleted.HasValue)
//                query = query.Where(role => role.IsDeleted == finder.IsDeleted.Value);

//            // apply entity id
//            if (finder.EntityId.HasValue && finder.EntityId != Guid.Empty)
//                query = query.Where(role => role.EntityId == finder.EntityId);

//            // apply revision id
//            if (finder.RevisionId.HasValue && finder.RevisionId != 0)
//                query = query.Where(role => role.RevisionId == finder.RevisionId);

//            // apply name
//            if (!string.IsNullOrWhiteSpace(finder.Name))
//                query = query.Where(e => finder.Name.Equals(e.Name, StringComparison.OrdinalIgnoreCase));

//            // apply establishment
//            if (finder.ForEstablishment != null)
//                if (finder.FallBackToDefault)
//                    query = !query.Any(e => e.Establishment.RevisionId == finder.ForEstablishment.RevisionId)
//                        ? query.Where(e => e.Establishment == null)
//                        : query.Where(e => e.Establishment.RevisionId == finder.ForEstablishment.RevisionId);
//                else
//                    query = query.Where(e => e.Establishment.RevisionId == finder.ForEstablishment.RevisionId);

//            // apply context tracking
//            if (!finder.IsForInsertOrUpdate)
//                query = query.AsNoTracking();

//            var found = query.ToList();
//            return found;
//        }

//        public EmailTemplate Get(EmailTemplateFinder finder)
//        {
//            return Find(finder).SingleOrDefault();
//        }

//        #endregion
//        #region EmailTemplate Aggregate Root

//        private IQueryable<EmailTemplate> All
//        {
//            get { return DbContext.EmailTemplates; }
//        }

//        public IQueryable<EmailTemplate> Current
//        {
//            get
//            {
//                return All
//                    .Include(t => t.Establishment)
//                .Current();
//            }
//        }

//        //public override IQueryable<EmailTemplate> AllIncluding(params Expression<Func<EmailTemplate, object>>[] includeProperties)
//        //{
//        //    return includeProperties.Aggregate<Expression<Func<EmailTemplate, object>>, IQueryable<EmailTemplate>>(
//        //        DbContext.EmailTemplates, (current, includeProperty) => current.Include(includeProperty));
//        //}

//        //public EmailTemplate Find(int id)
//        //{
//        //    return DbContext.EmailTemplates.Find(id);
//        //}

//        public EmailTemplate Revise(EmailTemplate revisableEntity)
//        {
//            throw new NotImplementedException();
//        }

//        #endregion
//    }
//}